package com.example.demo.service

import com.example.demo.constant.AddressConstant
import com.example.demo.singleton.VertxHolder
import io.vertx.core.eventbus.Message
import io.vertx.core.json.JsonArray
import io.vertx.core.json.JsonObject
import io.vertx.kotlin.coroutines.awaitResult

/**
 * @author Xu Haidong
 * @date 2023/4/23 13:59
 */
private val eventBus = VertxHolder.getInstance()?.eventBus()

suspend fun getRoleById(id: String): JsonObject? {
  val sql = "select * from sys_role where role_id=#{id}"
  val params = JsonObject().put("id", id)
  val reply = awaitResult<Message<JsonArray>> {
    eventBus?.request(AddressConstant.DB_QUERY_PARAMS, JsonObject().put("sql", sql).put("params", params), it)
  }
  val respBody = reply.body()
  return if (respBody.isEmpty) null else respBody.getJsonObject(0)
}

suspend fun getAll(): JsonArray {
  val sql = "select * from sys_role"
  val reply = awaitResult<Message<JsonArray>> { eventBus?.request(AddressConstant.DB_QUERY, sql, it) }
  return reply.body()
}
